package light.head.controller.oa;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.ConsumingDAO;
import light.head.dao.oa.ConsumingDetailDAO;
import light.head.dao.oa.ConsumingItemDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Consuming;
import light.head.model.ConsumingDetail;
import light.head.model.ConsumingItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 我的物品领用信息
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_MY_CONSUMING)
public class MyConsumingController extends SupporterController{
	
	
	private ConsumingDAO conDAO;
	private ConsumingItemDAO itemDAO;
	private ConsumingDetailDAO detailDAO;
 
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response)throws Exception{
		return new ModelAndView(Url.MY_CONSUMING_INIT);
	}
	 
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response)throws Exception{
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.CN, Parm.CT,Parm.DELETED);
		if (RESULT.SUCCESS == conDAO.finfBycc(cu_code(request),pb, map)) {
			List<Object> cc = new ArrayList<Object>();
			for (Object o : pb.getResultList()) {
				Consuming con = (Consuming) o ;
				cc.add(con.getCode());
			}
			List<ConsumingItem> ciList = itemDAO.findByConCodes(cc);
			if (null != ciList && ciList.size() > 0) {
				for (Object o : pb.getResultList()) {
					Consuming con = (Consuming) o ;
					con.setItemList(new ArrayList<ConsumingItem>());
					for (ConsumingItem item :ciList) {
						if (con.getCode().intValue() == item.getCoc().intValue()) {
							con.getItemList().add(item);
						}
					}
				}
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MY_CONSUMING_LIST);
	}
 
 
	/**
	 * 显示标签
	 */
	@RequestMapping(Url.SHOW_TAG)
	public ModelAndView showTag(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (code != null) {
			Consuming con = conDAO.get(code);
			if (con != null) {
				// 获得所有明细
				List<ConsumingItem> itemList = itemDAO.findByConCode(code);
				// 获取所有详情
				List<ConsumingDetail> detailList = detailDAO.findByRCode(code);
				
				// 循环将对应的详情放到对应的明细里面
				for (ConsumingItem item : itemList) {
						item.setDetailList(new ArrayList<ConsumingDetail>());
					for (ConsumingDetail detail : detailList) {
						// 判断明细的 编号 和 详情的编号相等的就放到对应的明细里面
						if (item.getCode().intValue() == detail.getCic().intValue()) {
							item.getDetailList().add(detail);
						}
					}
				}
				con.setItemList(itemList);
				request.setAttribute(Parm.INFO, con);
			}
		}
		return new ModelAndView(Url.MY_CONSUMING_SHOW_TAG);
	}
 
	 @Autowired
	public void setConDAO(ConsumingDAO conDAO) {
		this.conDAO = conDAO;
	}
	 @Autowired
	public void setItemDAO(ConsumingItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	 @Autowired
	public void setDetailDAO(ConsumingDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}
	 
 
 
}
